package com.jiezuo.flowable.app;

import com.jiezuo.flowable.entity.MyGroup;
import com.jiezuo.common.util.StringUtil;
import com.jiezuo.flowable.service.MyGroupService;
import org.flowable.ui.common.model.GroupRepresentation;
import org.flowable.ui.common.model.ResultListDataRepresentation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 *@Description 重写Flowable 下拉分组
 *    flowable-ui-modeler-rest-6.5.0.jar->org.flowable.ui.modeler.rest->EditorGroupsResource
 *@Author 庞留杰
 *@Date2020/9/14 10:11
 *@Version V1.0
 **/
@RestController
@RequestMapping("/app")
public class EditorGroupsResource {

    /* @Autowired
     MyService myService;*/
    @Autowired
    MyGroupService groupService;

    /**
     * 功能描述: 用于Flowable Modeler分组下拉
     * @author: 庞留杰
     * @exception
     * @date: 2020/9/9 17:01
     */
    @RequestMapping(value = "/rest/editor-groups", method = RequestMethod.GET)
    public ResultListDataRepresentation getGroups(@RequestParam(required = false, value = "filter") String filter) {
         /* 查询FLowable自带表act_id_user
            if (StringUtils.isNotBlank(filter)) {
                filter = filter.trim();
                String sql = "select * from act_id_group where NAME_ like #{name}";
                filter = "%" + filter + "%";
                List<Group> groups = idmIdentityService.createNativeGroupQuery().sql(sql).parameter("name", filter).listPage(0, 10);
                List<GroupRepresentation> result = new ArrayList<>();
                for (Group group : groups) {
                    result.add(new GroupRepresentation(group));
                }
                return new ResultListDataRepresentation(result);
            }
            return null;
        */
        // 此处重写
        /*List<Map<String, Object>> buttons = myService.findButtons(filter);
        List<GroupRepresentation> result = new ArrayList();
        for (Map group : buttons) {
            GroupRepresentation groupRepresentation = new GroupRepresentation();
            groupRepresentation.setId(group.get("id") + "");
            String name = "";
            if (StringUtil.isNotEmpty(group.get("description") + "")) {
                name = group.get("resource_name") + "【" + group.get("description") + "】";
            } else {
                name = group.get("resource_name") + "";
            }
            groupRepresentation.setName(name);
            groupRepresentation.setType(null);
            result.add(groupRepresentation);
        }*/
        MyGroup myGroup = new MyGroup();
        myGroup.setName(filter);
        List<MyGroup> list = groupService.findList(myGroup);
        List<GroupRepresentation> result = new ArrayList();
        for (MyGroup group : list) {
            GroupRepresentation groupRepresentation = new GroupRepresentation();
            groupRepresentation.setId(group.getId());
            String name = group.getName();
            groupRepresentation.setName(name);
            groupRepresentation.setType(null);
            result.add(groupRepresentation);
        }
        return new ResultListDataRepresentation(result);
    }

}
